package com.yunkaweilai.android.activity.member;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.model.AppRuleModel;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.model.MemberAlbumModel;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.model.member.MemberInfoModel;
import com.yunkaweilai.android.net.Glide.GlideUtils;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.popup.ImageEnlageAlbumPopup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 会员基本信息
 */
public class MemberInfoActivity extends BaseActivity {

    @BindView(R.id.id_img_head)
    ImageView idImgHead;
    @BindView(R.id.id_tv_name)
    TextView idTvName;
    @BindView(R.id.id_tvcard_number)
    TextView idTvcardNumber;
    @BindView(R.id.id_tv_card_infor)
    TextView idTvCardInfor;
    @BindView(R.id.id_tv_card_no)
    TextView idTvCardNo;
    @BindView(R.id.id_tv_confirm_card)
    TextView idTvConfirmCard;
    @BindView(R.id.id_tv_member_lever)
    TextView idTvMemberLever;
    @BindView(R.id.id_tv_shop_name)
    TextView idTvShopName;
    @BindView(R.id.id_tv_time)
    TextView idTvTime;
    @BindView(R.id.id_tv_sex)
    TextView idTvSex;
    @BindView(R.id.id_tv_tel)
    TextView idTvTel;
    @BindView(R.id.id_tv_brithday)
    TextView idTvBrithday;
    @BindView(R.id.id_tv_anniversary)
    TextView idTvAnniversary;
    @BindView(R.id.id_tv_recommender)
    TextView idTvRecommender;
    @BindView(R.id.id_tv_salesman)
    TextView idTvSalesman;
    @BindView(R.id.id_tv_remarks)
    TextView idTvRemarks;
    @BindView(R.id.id_llayout_diy)
    LinearLayout idLlayoutDiy;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    @BindView(R.id.id_views)
    View idViews;

    public static final String MEMBER_INFO_MODEL = "MEMBER_INFO_MODEL";
    public static final String MEMBER_INFO_BUNDLE = "MEMBER_INFO_BUNDLE";
    public static final String TYPE = "TYPE";
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.id_tv_car_no)
    TextView idTvCarNo;
    @BindView(R.id.content_view)
    ScrollView contentView;
    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.id_img1)
    ImageView idImg1;
    @BindView(R.id.id_img2)
    ImageView idImg2;
    @BindView(R.id.id_img3)
    ImageView idImg3;
    @BindView(R.id.id_img4)
    ImageView idImg4;
    @BindView(R.id.id_llayout_first_img)
    LinearLayout idLlayoutFirstImg;
    @BindView(R.id.id_img5)
    ImageView idImg5;
    @BindView(R.id.id_img6)
    ImageView idImg6;
    @BindView(R.id.id_llayout_next_img)
    LinearLayout idLlayoutNextImg;

    private MemberHeadModel model;
    private MemberInfoModel memberInfoModel;
    private LayoutInflater inflater;
    private ArrayList<MemberAlbumModel> member_album;
    private AppRuleModel.DataBean.InfoBean appRuleModel;
    private String Type = "1";

    public static void actionStart(Context context, MemberHeadModel model) {
        Intent intent = new Intent(context, MemberInfoActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(MEMBER_INFO_MODEL, model);
        intent.putExtra(MEMBER_INFO_BUNDLE, bundle);
        context.startActivity(intent);
    }

    //type = 1 右上角有修改，除此之外都没有
    public static void actionStart(Context context, MemberHeadModel model, String Type) {
        Intent intent = new Intent(context, MemberInfoActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(MEMBER_INFO_MODEL, model);
        intent.putExtra(MEMBER_INFO_BUNDLE, bundle);
        intent.putExtra(TYPE, Type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_info);
        ButterKnife.bind(this);
        inflater = this.getLayoutInflater();
        model = (MemberHeadModel) getIntent().getBundleExtra(MEMBER_INFO_BUNDLE).getSerializable(MEMBER_INFO_MODEL);
        Type = getIntent().getStringExtra(TYPE);
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        appRuleModel = BaseApplication.getAppRuleModel();
        initHttp();
        initHeadView();
        initClick();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onShopEvent(Event.TypeEvent event) {
        if (event != null) {
            if (event.type == CommonConstants.MEMBER_LIST) {
                if (event.flag) {
                    idMultipleStatusView.showLoading();
                    initHttp();
                }
            }
        }
    }

    private void initClick() {
        idImgHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.ImageEnlage(mAc, "1", model.getMember_avatar(), model.getMember_sex());
            }
        });
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.MEMBER_INFO_BASIC)
                .addParams("member_id", model.getId())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean codeInfoRs = Utils.checkCode(mAc, json);
                        if (codeInfoRs) {
                            Gson gson = new Gson();
                            memberInfoModel = gson.fromJson(json, MemberInfoModel.class);
                            model = memberInfoModel.getData().getList();
                            initView();
                            initHeadView();
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showContent();
                            }
                        } else {
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }

    //会员基本信息
    private void initView() {
        MemberHeadModel memberinfo = memberInfoModel.getData().getList();
        idTvCardNo.setText(memberinfo.getCard_no() + "");
        idTvConfirmCard.setText(memberinfo.getSparecard_no() + "");
        idTvMemberLever.setText(memberinfo.getLevel_name() + "");
        idTvShopName.setText(memberinfo.getSub_store_name() + "");
        if ("1".equals(memberinfo.getIs_alwaysvalid())) {
            idTvTime.setText("永久有效");
        } else {
            idTvTime.setText(memberinfo.getValidtime() + "");
        }
        if ("famale".equals(memberinfo.getMember_sex())) {
            idTvSex.setText("女");
        } else if ("male".equals(memberinfo.getMember_sex())) {
            idTvSex.setText("男");
        } else {
            idTvSex.setText("未知");
        }
        idTvTel.setText(memberinfo.getMember_tel() + "");
        idTvBrithday.setText(memberinfo.getMember_birthday() + "");
        idTvAnniversary.setText(memberinfo.getMember_anniversary() + "");
        if (StringUtils.isEmpty(memberinfo.getRecommend_name())) {
            idTvRecommender.setText("无");
        } else {
            idTvRecommender.setText(memberinfo.getRecommend_name() + "");
        }
        if (StringUtils.isEmpty(memberinfo.getCommission_name())) {
            idTvSalesman.setText("无");
        } else {
            idTvSalesman.setText(memberinfo.getCommission_name() + "");
        }
        idTvRemarks.setText(memberinfo.getMember_remarks() + "");
        idTvCarNo.setText(memberinfo.getPlate_number() + "");
        initAddView();
        initAlbumView();
    }

    //会员相册
    private void initAlbumView() {
        member_album = memberInfoModel.getData().getList().getMember_album();
        if (member_album.size() > 0) {
            idLlayoutFirstImg.setVisibility(View.VISIBLE);
        } else {
            idLlayoutFirstImg.setVisibility(View.GONE);
        }
        if (member_album.size() > 4) {
            idLlayoutNextImg.setVisibility(View.VISIBLE);
        } else {
            idLlayoutNextImg.setVisibility(View.GONE);
        }
        switch (member_album.size()) {
            case 1:
                idImg1.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(0).getPath(), idImg1);
                idImg2.setVisibility(View.GONE);
                idImg3.setVisibility(View.GONE);
                idImg4.setVisibility(View.GONE);
                idImg5.setVisibility(View.GONE);
                idImg6.setVisibility(View.GONE);

                break;
            case 2:
                idImg1.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(0).getPath(), idImg1);
                idImg2.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(1).getPath(), idImg2);
                idImg3.setVisibility(View.GONE);
                idImg4.setVisibility(View.GONE);
                idImg5.setVisibility(View.GONE);
                idImg6.setVisibility(View.GONE);
                break;
            case 3:
                idImg1.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(0).getPath(), idImg1);
                idImg2.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(1).getPath(), idImg2);
                idImg3.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(2).getPath(), idImg3);
                idImg4.setVisibility(View.GONE);
                idImg5.setVisibility(View.GONE);
                idImg6.setVisibility(View.GONE);
                break;
            case 4:
                idImg1.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(0).getPath(), idImg1);
                idImg2.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(1).getPath(), idImg2);
                idImg3.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(2).getPath(), idImg3);
                idImg4.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(3).getPath(), idImg4);
                idImg5.setVisibility(View.GONE);
                idImg6.setVisibility(View.GONE);
                break;
            case 5:
                idImg1.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(0).getPath(), idImg1);
                idImg2.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(1).getPath(), idImg2);
                idImg3.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(2).getPath(), idImg3);
                idImg4.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(3).getPath(), idImg4);
                idImg5.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(4).getPath(), idImg5);
                idImg6.setVisibility(View.GONE);
                break;
            case 6:
                idImg1.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(0).getPath(), idImg1);
                idImg2.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(1).getPath(), idImg2);
                idImg3.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(2).getPath(), idImg3);
                idImg4.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(3).getPath(), idImg4);
                idImg5.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(4).getPath(), idImg5);
                idImg6.setVisibility(View.VISIBLE);
                GlideUtils.loadImage(mAc, member_album.get(5).getPath(), idImg6);
                break;
        }
    }

    private void initAddView() {
        idLlayoutDiy.removeAllViews();
        List<MemberInfoModel.DataBean.DiyfieldListBean> diyfield_list = memberInfoModel.getData().getDiyfield_list();
        for (int i = 0; i < diyfield_list.size(); i++) {
            MemberInfoModel.DataBean.DiyfieldListBean diyfieldListBean = diyfield_list.get(i);
            LinearLayout view = (LinearLayout) inflater
                    .inflate(R.layout.view_member_info_diy_txt, null);
            ((TextView) view.findViewById(R.id.id_tv_name)).setText(diyfieldListBean.getField_name() + "");
            ((TextView) view.findViewById(R.id.id_tv_content)).setText(diyfieldListBean.getDiyfield_contents() + "");
            idLlayoutDiy.addView(view);
        }
    }

    //会员头部信息
    private void initHeadView() {
        TitleBuilder titleBuilder = new TitleBuilder(mAc).setTitleText("会员详情").setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                }).setRightOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if ((appRuleModel.isIs_super() || appRuleModel.getApp_rule().get("18") )&& Type.equals("1")) {
                            if (model.is_edit()) {
                                UpdateMemberInfoActivity.actionStart(mAc, model);
                            } else {
                                showToast(R.string.member_updata_tip);
                            }
                        }

                    }
                });
        if ((appRuleModel.isIs_super() || appRuleModel.getApp_rule().get("18") )&& Type.equals("1")) {
            titleBuilder.setRightText("修改");
        }
        Utils.MemberHeadImg(mAc, model.getMember_avatar(), model.getMember_sex(), idImgHead);

        idTvName.setText(model.getMember_name() + " (" + model.getLevel_name() + ": " + model.getCard_no() + ")");
        idTvCardInfor.setText("折扣：" + model.getDiscount() / 10 + "折   积分：" + model.getMember_points() + "   余额：" + model.getMember_price());

    }

    @OnClick({R.id.id_img1, R.id.id_img2, R.id.id_img3, R.id.id_img4, R.id.id_img5, R.id.id_img6})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_img1:
                if (member_album.size() > 0) {
                    ImageEnlageAlbumPopup popup = new ImageEnlageAlbumPopup(mAc, member_album.get(0).getPath(), R.mipmap.ic_image_add, member_album.get(0).getAlbum_remark());
                    popup.showPopupWindow();
                }
                break;
            case R.id.id_img2:
                if (member_album.size() > 1) {
                    ImageEnlageAlbumPopup popup = new ImageEnlageAlbumPopup(mAc, member_album.get(1).getPath(), R.mipmap.ic_image_add, member_album.get(1).getAlbum_remark());
                    popup.showPopupWindow();
                }
                break;
            case R.id.id_img3:
                if (member_album.size() > 2) {
                    ImageEnlageAlbumPopup popup = new ImageEnlageAlbumPopup(mAc, member_album.get(2).getPath(), R.mipmap.ic_image_add, member_album.get(2).getAlbum_remark());
                    popup.showPopupWindow();
                }
                break;
            case R.id.id_img4:
                if (member_album.size() > 3) {
                    ImageEnlageAlbumPopup popup = new ImageEnlageAlbumPopup(mAc, member_album.get(3).getPath(), R.mipmap.ic_image_add, member_album.get(3).getAlbum_remark());
                    popup.showPopupWindow();
                }
                break;
            case R.id.id_img5:
                if (member_album.size() > 4) {
                    ImageEnlageAlbumPopup popup = new ImageEnlageAlbumPopup(mAc, member_album.get(4).getPath(), R.mipmap.ic_image_add, member_album.get(4).getAlbum_remark());
                    popup.showPopupWindow();
                }
                break;
            case R.id.id_img6:
                if (member_album.size() > 5) {
                    ImageEnlageAlbumPopup popup = new ImageEnlageAlbumPopup(mAc, member_album.get(5).getPath(), R.mipmap.ic_image_add, member_album.get(5).getAlbum_remark());
                    popup.showPopupWindow();
                }
                break;
        }
    }
}
